// 基础地址 设置成常数
const BASE_URL = 'http://110.41.43.118:3008';

(function(){
	
	let oUsername = document.querySelector('.username');
	let oPassword = document.querySelector('.password');
	let oLoginBtn = document.querySelector('.login_btn');
	
	// 点击登录按钮
	oLoginBtn.onclick = function(){
		// console.log(oLoginBtn);
		if(oUsername.value == '' || oPassword.value == ''){
			alert('用户名和密码不能为空');
			return;
		};
		
		lb.ajax({
			method : 'post',
			url : BASE_URL + '/api_user',
			data : {username: oUsername.value, password: oPassword.value, status: 'login'},
			success(res){
				console.log(res);
				if( res.code != 0){
					alert('用户名或密码错误');
					return;
				}
				// 登录成功，存储登录状态
				window.localStorage.setItem('username', res.username);
				window.localStorage.setItem('token', res.user_id);
				// 分情况跳转 首页 详情
				let goodsId = lb.getUrlValue('goodsId');
				let catId = lb.getUrlValue('catId');
				if(goodsId){
					window.location.href = `product.html?goodsId=${goodsId}&catId=${catId}`;
				}else{
					// 跳转到首页
					window.location.href = 'index.html';
				}
				
			}
		})
	};
})();